package com.seari.service.impl;

import com.seari.bean.*;
import com.seari.mapper.EquipmentMapper;
import com.seari.mapper.UserMapper;
import com.seari.mapper.UserRoleMapper;
import com.seari.pojo.QueryUser;
import com.seari.service.EquipmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class EquipmentServiceImpl implements EquipmentService {

//    @Autowired
//    private MaterialCaregoryMapper materialCaregoryMapper;
//    @Autowired
//    private MaterialCaregoryAttrValueMapper materialCaregoryAttrValueMapper;
//    @Autowired
//    private MaterialCaregoryAttrMapper materialCaregoryAttrMapper;
//    @Autowired
//    private ProductMapper productMapper;
//    @Autowired
//    private DevLifeMapper devLifeMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private EquipmentMapper equipmentMapper;
    private Logger Log= LoggerFactory.getLogger(this.getClass());
    @Override
    public List<MaterialCaregory> getFMcTree(MaterialCaregory materialCaregory) {
        return equipmentMapper.getFMcTree(materialCaregory);
    }

    @Override
    public List<MaterialCaregory> getFMcByParams(MaterialCaregory materialCaregory) {
        return equipmentMapper.getFMcByParams(materialCaregory);
    }

    @Override
    public MaterialCaregory getFMcByFMcId(MaterialCaregory materialCaregory) {
        return equipmentMapper.getFMcByFMcId(materialCaregory);
    }

    @Override
    public int deleteMc(MaterialCaregory materialCaregory) {
        return equipmentMapper.deleteMc(materialCaregory);
    }

    @Override
    public List<Map<String, String>> getLineList(Map<String, String> map) {
        return equipmentMapper.getLineList(map);
    }

    @Override
    public List<Station> getStationByParam(Station station) {
        return equipmentMapper.getStationByParam(station);
    }

    @Override
    public List<MaterialCaregoryAttr> getFMcAttrByParams(MaterialCaregoryAttr materialCaregoryAttr) {
        return equipmentMapper.getFMcAttrByParams(materialCaregoryAttr);
    }

    @Override
    public List<MaterialCaregoryAttr> getAttrAndValueByFMcId(MaterialCaregoryAttr materialCaregoryAttr) {
        return equipmentMapper.getAttrAndValueByFMcId(materialCaregoryAttr);
    }

    @Override
    public int addMcAttrValueBatch(List<MaterialCaregoryAttrVaule> materialCaregoryValueAttrList) {
        return equipmentMapper.addMcAttrValueBatch(materialCaregoryValueAttrList);
    }

    @Override
    public List<Product> getProductByFMcId(Product product) {
        return equipmentMapper.getProductByFMcId(product);
    }

    @Override
    public List<Product> getProductById(Product product) {
        return equipmentMapper.getProductById(product);
    }

    @Override
    public int addProductBatch(List<Product> productList) {
        return equipmentMapper.addProductBatch(productList);
    }



    @Override
    public void saveDevLifeInfo(DevLifeBean devLife) {
        equipmentMapper.saveDevLifeInfo(devLife);
    }

    @Override
    public User getUserByLogin(String username, String password)
    {
        return userMapper.getUserByLogin(username, password);
    }


    @Override
    public List<User> getUserByUid(QueryUser qu){
        return userMapper.getUserByUid(qu);
    }

    @Override
    public int importEtInfo(List<Equipment> equipment) {
        List<Equipment> equipments = new ArrayList<>();
            for (int i=0; i < equipment.size();i++) {
                Equipment equipment1 = equipment.get(i);
                char is_plan = equipment1.getIs_plan();
                int task_num = equipment1.getTask_num();
                if(is_plan == '0'||is_plan==0){
                    equipments.add(equipment1);
                }
            }
        if(equipments.size()<=0){return equipmentMapper.importEtInfo(equipment);}
        else {return equipmentMapper.importEtInfo(equipments);}
    }

    @Override
    public int importEtInfoPlan(List<Equipment> equipment) {
        List<Equipment> equipment1 = equipment;
        if (equipment1 != null && equipment1.size()!=0) {
//            for (int j=0; j< equipment1.size();j++) {
//                Equipment equipment2 = equipment1.get(j);
//                char is_plan = equipment2.getIs_plan();
//                Date maintain_next_time = equipment2.getMaintain_next_time();
//                if(is_plan=='0'||is_plan == 0||maintain_next_time==null){
//                    equipment1.remove(j);
//                }
//            }
            Iterator<Equipment> equipmentIterator = equipment1.iterator();
            while (equipmentIterator.hasNext()){
                Equipment next = equipmentIterator.next();
                char is_plan = next.getIs_plan();
                if(is_plan=='0'|| is_plan==0||next.getMaintain_next_time()==null){
                    equipmentIterator.remove();
                }
            }
        }
        if(equipment1==null||equipment1.size()<=0){return 0;}
        else {return equipmentMapper.importEtInfoPlan(equipment1);}
    }

    @Override
    public int saveEtInfoPlan(Equipment equipment) {
        return equipmentMapper.saveEtInfoPlan(equipment);
    }

    @Override
    public int saveEtInfo(Equipment equipment) {
        return equipmentMapper.saveEtInfo(equipment);
    }

    @Override
    public List<User> getUserByParams(QueryUser qu)
    {
        return userMapper.getUserByParams(qu);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addUser(User user)
    {
        return userMapper.addUser(user);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateUser(User user)
    {
        userMapper.updateUser(user);
        userRoleMapper.deleteUserRoleByUuid(user.getUid());
        UserRole userRole = new UserRole();
        userRole.setUuid(user.getUid().intValue());
        userRole.setUurid(user.getUrids());
        return userRoleMapper.apportionRoles(userRole);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteUser(int uid)
    {
        userRoleMapper.deleteUserRoleByUuid((long)uid);
        //System.out.println(1 / 0);
        return userMapper.deleteUser(uid);
    }
}
